
<div class="wrapper wrapper-content page-heading animated fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Report</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                            <div class="ibox-title" style="border: none;">
                              <h5 style="color: #00afef"><i>Billing Progress Status</i></h5>
                            </div>
                           <div class="ibox-content">
                           <div class="table-responsive">
                           <form id="search_bill_prog">
                           <div class="form-group">
                                <label style="text-align: right;" class="col-lg-4 control-label">Bill Year-Month</label>
                                <div class="col-lg-4 input-daterange input-group">
                                    <select name="year" class="form-control-custom">
                                      <?php
                                      $mon = array('','January','February','March','April','May','June','July','August','September','October','November','December');
                                      for($i = date('Y'); $i >= 2010 ; $i--):?>
                                        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                      <?php endfor; ?>
                                    </select>
                                    <span class="input-group-addon">-</span>
                                   <select name="month" class="form-control-custom">
                                      <?php for($o = 1; $o <= 12; $o++):?>
                                        <option value="<?php echo $o < 10 ? '0'.$o : $o; ?>"><?php echo $mon[$o]; ?></option>
                                      <?php endfor; ?>
                                   </select>
                                </div>
                            </div>
                           </form>
                           </div>
                          <center><button class="btn btn-primary btn-outline btn-md m-t-n-xs" id="get-bill" type="button"><i class="fa fa-check fa-fw"></i> Get Status</button></p></center>
                           </div><!-- END OF IBOX CONTENT -->
                           <div id="result" style="display: none;">
                              <div class="ibox-content">
                                <div class="row">
                                    <div class="col-xs-9">
                                    <div class="btn-group">
                                      <a style="display:none;" id="to_pdf" href="#" class="btn btn-link" ><i class="fa fa-file-pdf-o fa-fw"></i> <small>Export as PDF</small></a>
                                      <a style="display:none;" id="to_excel" href="#" class="btn btn-link" ><i class="fa fa-file-excel-o fa-fw"></i> <small>Export as Excel</small></a>
                                      <button style="display:none;" id="to_mail" class="btn btn-link" ><i class="fa fa-envelope-o fa-fw"></i> <small>Send to Email</small></button>
                                  </div>
                                     <!--  <div class="col-xs-3">
                                        <a id="to_pdf" class="btn btn-w-m btn-outline btn-link" href="#"><i class="fa fa-file-pdf-o fa-fw"></i> <small>Export as PDF</small></a>
                                      </div>
                                      <div class="col-xs-3">
                                        <a id="to_excel" class="btn btn-w-m btn-outline btn-link" href='#'><i class="fa fa-file-excel-o fa-fw"></i> <small>Export as Excel</small></a>
                                      </div>
                                      <div class="col-xs-3">
                                        <div class="">
                                            <button class="btn btn-w-m btn-outline btn-link dropdown-toggle" id="to_mail"><i class="fa fa-envelope-o fa-fw"></i> <small>Send to Email</small></button>
                                        </div>
                                      </div> -->
                                    </div>
                                </div>
                             </div><!-- END OF IBOX CONTENT -->
                             <br>
                             <p id="search_values"></p>
                             <div class="table-responsive">
                              <table id="dataTAble" class="table table-striped table-hover">
                                <thead>
                                  <tr>
                                    <th>Zone</th>
                                    <th>Book</th>
                                    <!-- <th data-sort-ignore="true" style="display:none;">Zone 2</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 3</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 4</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 5</th> -->
                                    <th>Act Custs</th>
                                    <th>Bill Complete</th>
                                    <th>Bill Pending</th>
                                    <th>% Comp.</th>
                                    <th>Bill-Water</th>
                                    <th>Bill-Chrgs</th>
                                    <th>Total Bill Amt</th>
                                    <th>Qty m<sup>3</sup></th>
                                  </tr>
                                </thead>
                                <tbody id="bill_table">
                                </tbody>
                                <tfoot id="bill_table_foot">
                                </tfoot>
                              </table>
                              </div>
                           </div>
                           <div class="ibox-content" id="email_html" style="display: none;">
                                  <form class="form-horizontal" id="email_form">
                                      <div class="form-group"><label class="col-sm-2 control-label">From:</label>
                                          <div class="col-sm-10"><input name="email_from" type="text" class="form-control" value="no-reply@ums.com"></div>
                                      </div>
                                      <div class="form-group"><label class="col-sm-2 control-label">To:</label>
                                          <div class="col-sm-10">
                                              <input name="email_to" type="text" class="form-control" value="">
                                            <!-- <div class="input-group">
                                              <input name="email_to" type="text" class="form-control" value="">
                                              <span class="input-group-btn"><button style="padding: 4px 12px; border-radius: none;" type="button" class="btn btn-white"><i class="fa fa-group fa-fw"></i></button></span>
                                            </div> -->
                                          </div>
                                      </div>
                                      <div class="form-group"><label class="col-sm-2 control-label">Subject:</label>
                                          <div class="col-sm-10"><input name="email_subj" type="text" class="form-control" value=""></div>
                                      </div>
                                       <div class="form-group"><label class="col-sm-2 control-label">File Name:</label>
                                          <div class="col-sm-10"><input name="email_filename" type="text" class="form-control" value=""></div>
                                      </div>
                                      <div class="form-group"><label class="col-sm-2 control-label">File Type:</label>
                                         <div class="col-sm-10"><label class="checkbox-inline">
                                          <input type="checkbox" name="email_topdf" value="pdf" checked> PDF (.pdf) </label> <label class="checkbox-inline">
                                          <input type="checkbox" name="email_toexcel" value="excel" > EXCEL (.xls) </label>
                                          <input style="border: none; width:0px" readonly type="text" name="email_error" value="">
                                        </div>
                                      </div>
                                       <div class="form-group"><label class="col-sm-2 control-label">Message:</label>
                                          <div class="col-sm-10">
                                            <div id="summernote"></div>
                                          </div>
                                      </div>
                                      <div class="form-group"><label class="col-sm-2 control-label"></label>
                                         <div class="col-sm-10">
                                           <button type="button" class="btn btn-white btn-md m-t-n-xs pull-right" id="email_cancel">Close</button>
                                           <button type="button" class="btn btn-success btn-md m-t-n-xs pull-right btn-margin-right" id="email_send">Send</button>
                                        </div>
                                      </div>
                                  </form>
                              </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<script>
      $(document).ready(function(){
        // $("select").chosen({width: "100%"});
        $('#data_5 .input-daterange').datepicker({
            keyboardNavigation: false,
            forceParse: false,
            autoclose: true,
            format: "yyyy-mm-dd"
        });
        $(function(){
            $('#scroll').slimScroll({
              height: '250px'
            });
        });
      $('.email_type').click(function(){
        sessionStorage.type = $(this).attr('id');
        console.log(sessionStorage.type);
      });
      $()
        // $('.footable').footable();
       $('#summernote').summernote({
          disableDragAndDrop: true,
          shortcuts: false,
          height: 200,
          placeholder: 'write here...',
          toolbar: [
            ['para', ['ul', 'ol']]
          ]
        });

       $('#to_mail').click(function(){
           $('#result').slideUp();
           $('#email_html').show();
           $('input[name=email_filename]').val('Collection_Details_Report');
           //window.open("<?php echo base_url($this->session->userdata('forajax')); ?>/mail_window","Send Email", "height=500,width=500");
       });
       $('#email_cancel').click(function(){
          $('#email_html').hide();
          $('#result').slideDown();
       });

        $('#email_send').click(function(){
          if($("#email_form").valid() == true && fileType() == true){
            $.ajax({
              url: "<?php echo base_url($this->session->userdata('forajax')); ?>/send_mail",
              type: 'POST',
              dataType: 'json',
              data: $('#email_form').serialize()+'&'+$.param({ 'email_message': $('#summernote').code(), 'range1' : sessionStorage.range1, 'range2': sessionStorage.range2,'date_1' : sessionStorage.date_1, 'date_2' :sessionStorage.date_2 }),
              success: function(data){
                if(data.msg == 'success'){
                  notifymsg('success','Email Send');
                  $('#email_html').hide();
                  $('#result').slideDown();
                }else{
                  notifymsg('danger','Error Sending Mail');
                }
                $('#email_send').html('Send');
                $('#email_send').removeAttr('disabled');
              },
              beforeSend: function(){
                $('#email_send').html('Sending Mail..');
                $('#email_send').attr('disabled',true);
              }
            });
          }
        });

        $('#search_role_form').keydown(function (e) {
          if (e.which == 13) {
            $('#get-bill').trigger('click');
            return false;
          }
        });

        $(document).on('click','.open_pending',function(){
          console.log($(this).attr('date'));
          console.log($(this).attr('zone'));
          var title = 'Pending List';
          var url = '<?php echo base_url($this->session->userdata('forajax')); ?>/get_pend?date='+$(this).attr('date')+'&zone='+$(this).attr('zone')+'&zone2='+$(this).attr('zone2');
          window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no');
          //window.open("<?php echo base_url($this->session->userdata('forajax')); ?>/pending_list","Send Email", "height=800,width=500");
        });

        $('#dataTAble').dataTable();

        $('#get-bill').click(function(){
          $('#dataTAble').dataTable().fnDestroy();
          $('#bill_table_foot').html('');
          $('#bill_table').html('');
          $('#email_html').hide();
          $('#email_form')[0].reset();
          $('#search_values').html('Bill Year-Month <strong>' + $('select[name=year]').val() + '</strong>-<strong>' + $('select[name=month]').val());
          $.ajax({
            url: "<?php echo base_url($this->session->userdata('forajax')); ?>/get_status",
            type: 'POST',
            dataType: 'json',
            data: $('#search_bill_prog').serialize(),
            success: function(data){
              $('#get-bill').removeAttr('disabled');
              var tmp = "";
              var tmp2 = "";
              var count = data.length - 1;
              var t_bill_amt = 0;
              var t_custs_count = 0;
              var t_bill_count = 0;
              var t_pend_count = 0;
              var t_percent_avrg = 0;
              var t_bill_watcons = 0;
              var t_bill_chrg = 0;
              var t_qty = 0;
              var qty = 0;
              var bill_amt = 0;
              var bill_watcons = 0;
              var bill_chrg = 0;
              console.log(data);
              if(data.length == 0){
                 //$('#bill_table').html('<tr><td colspan="10" align="center">No Result</td></tr>');
                 $('#to_excel').hide();
                 $('#to_pdf').hide();
                 $('#to_mail').hide();
              }else{
                $.each(data,function(i,item){
                  if(data[i].bill_amt == null){
                    bill_amt = 0;
                  }else{
                    bill_amt = data[i].bill_amt;
                  }
                  if(data[i].bill_watcons == null){
                    bill_watcons = 0;
                  }else{
                    bill_watcons = data[i].bill_watcons;
                  }
                  if(data[i].bill_chrg == null){
                    bill_chrg = 0;
                  }else{
                    bill_chrg = data[i].bill_chrg;
                  }
                  if(data[i].qty == null){
                    qty = 0;
                  }else{
                    qty = data[i].qty;
                  }
                  /*<td style="display:none;">'+ data[i].zone_2 +'</td><td style="display:none;">'+ data[i].zone_3 +'</td><td style="display:none;">'+ data[i].zone_4 +'</td><td style="display:none;">'+ data[i].zone_5 +'</td>*/
                  tmp += '<tr><td>'+ data[i].zone_1 +'</td><td>'+ data[i].zone_2 +'</td><td align="right">'+ data[i].custs_count +'</td><td align="right">'+ data[i].bill_count +'</td><td align="right"><a style="padding:0px;color: #80BEF9;" href="#" date="'+$('select[name=year]').val()+'-'+$('select[name=month]').val()+'" zone="'+data[i].zone_1+'" zone2="'+data[i].zone_2+'"  class="btn btn-link open_pending">'+ data[i].pending_count +'</a></td><td align="right">'+ parseFloat(data[i].percent_completed).toFixed(2) +' %</td><td align="right">'+ parseFloat(bill_watcons).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</td><td align="right">'+ parseFloat(bill_chrg).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</td><td align="right">'+ parseFloat(bill_amt).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</td><td align="right">'+ parseFloat(qty).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</td></tr>';
                    t_bill_amt = t_bill_amt + parseFloat(bill_amt);
                    t_custs_count = t_custs_count + parseFloat(data[i].custs_count);
                    t_bill_count = t_bill_count + parseFloat(data[i].bill_count);
                    t_pend_count = t_pend_count + parseFloat(data[i].pending_count);
                    t_bill_watcons = t_bill_watcons + parseFloat(bill_watcons);
                    t_bill_chrg = t_bill_chrg + parseFloat(bill_chrg);
                    t_qty = t_qty + parseFloat(qty);
                });
                t_percent_avrg = parseFloat(t_bill_count) * 100 / parseFloat(t_custs_count);
                tmp2 = '<tr><td></td><td></td><td align="right"><b>'+ t_custs_count +'</b></td><td align="right"><b>'+ t_bill_count +'</b></td><td align="right"><b>'+ t_pend_count +'</b></td><td align="right"><b>'+ parseFloat(t_percent_avrg).toFixed(2) +' % </b></td><td align="right"><b>'+ parseFloat(t_bill_watcons).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</b></td><td align="right"><b>'+ parseFloat(t_bill_chrg).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</b></td><td align="right"><b>'+ t_bill_amt.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</b></td><td align="right"><b>'+ t_qty.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</b></td></tr>';
                $('#bill_table').html(tmp); //$('#bill_table').html(tmp).trigger('footable_redraw');
                $('#bill_table_foot').html(tmp2);
                $('#to_excel').attr('href','<?php echo base_url($this->session->userdata("forajax")); ?>/gen_excel?year='+$('select[name=year]').val()+'&month='+$('select[name=month]').val());
                $('#to_pdf').attr('href','<?php echo base_url($this->session->userdata("forajax")); ?>/gen_pdf?year='+$('select[name=year]').val()+'&month='+$('select[name=month]').val());
               // $('#to_excel').show();
                //$('#to_pdf').show();
                //$('#to_mail').show();
              }
               $('#dataTAble').DataTable( {
                    "order": [[ 1, "asc" ]],
                        "columnDefs": [ {
                          "targets": 'no-sort',
                          "orderable": false,
                    } ],
                    "sDom": 'Ttip',
                    "tableTools": {
                          "aButtons": [{
                                          "sExtends": "copy",
                                          "sButtonText": "Copy to clipboard"
                                      },
                                      {
                                          "sExtends": "xls",
                                          "sButtonText": "Export to Excel"
                                      }],
                          "sSwfPath": "<?php echo base_url() ?>assets/js/plugins/dataTables/swf/copy_csv_xls_pdf.swf"
                      }
              });
              $('#result').show();
            },
            beforeSend: function(){
              $('#get-bill').attr('disabled',true);
              $('#result').hide();
            },
            complete: function(){
               $('#get-bill').removeAttr('disabled');
               $('#result').show();
            }
          });
        });
      function fileType(){
        if($("input[name=email_toexcel").prop('checked') == false && $("input[name=email_topdf").prop('checked') == false){
          var validator = $( "#email_form" ).validate();
          validator.showErrors({
            "email_error": "Please select atleast 1 filetype."
          });
          return false;
        }else{
          return true;
        }
      }

      $("#email_form").validate({
           rules: {
               email_to: {
                  required: true,
                  email: true
               },
               email_subj: {
                  required: true
               },
               email_from: {
                  required: true
               },
               email_filename: {
                required: true
               }
           }
      });
      });
</script>